package mesweb.mesweb.controller.produceLine;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.ProduceLine.ProduceLineDto;
import mesweb.mesweb.entity.produceLine.ModelProduceLine;
import mesweb.mesweb.entity.produceLine.ModelProduceLineBranchLine;
import mesweb.mesweb.service.procedure.IMesProductProcedureService;
import mesweb.mesweb.service.produceLine.IModelProduceLineBranchLineService;
import mesweb.mesweb.service.produceLine.IModelProduceLineService;
import mesweb.mesweb.vo.produceLine.ModelProduceLineVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 产品线
 * @Author: jeecg-boot
 * @Date: 2021-07-04
 * @Version: V1.0
 */
@Api(tags = "产品产线")
@RestController
@RequestMapping("/mes/modelProduceLine")
public class ModelProduceLineController {
    @Autowired
    private IModelProduceLineService modelProduceLineService;

    @Autowired
    private IModelProduceLineBranchLineService modelProduceLineBranchLineService;

     @Autowired
     private IMesProductProcedureService mesProductProcedureService;

    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam(value = "name", required = false) String name) {
        List<ModelProduceLineVo> data = new ArrayList<>();
        QueryWrapper<ModelProduceLineVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
        }
        data = modelProduceLineService.getProduceLine(queryWrapper);
        return CommonResult.success(data);
    }

    /**
     * 分頁查詢
     * @param productProcedureNo
     * @param updateStartTime
     * @param updateEndTime
     * @param status
     * @param page
     * @param size
     * @param req
     * @return
     */
    @ApiOperation(value = "产品线-分页列表查询", notes = "产品线-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(@RequestParam(name = "no", required = false, defaultValue = "") String productProcedureNo,
                                         @RequestParam(name = "updateStartTime", required = false, defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date updateStartTime,
                                         @RequestParam(name = "updateEndTime", required = false, defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date updateEndTime,
                                         @RequestParam(name = "status", required = false, defaultValue = "-1") Integer status,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "size", defaultValue = "10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapper<ModelProduceLineVo> queryWrapper = new QueryWrapper<>();
        if (productProcedureNo != null && productProcedureNo.length() > 0) {
            queryWrapper.eq("e.Produce_Line_No", productProcedureNo);
        } else {
            if (updateStartTime != null && updateEndTime != null) {
                queryWrapper.between("e.updateTime", updateStartTime, updateEndTime);
            }
            if (status != -1) {
                queryWrapper.eq("e.status", status);
            }
        }
        Page<ModelProduceLineVo> pageModelProduceLineVo = new Page<ModelProduceLineVo>(page, size);
        IPage<ModelProduceLineVo> pageList = modelProduceLineService.getProduceLine(pageModelProduceLineVo, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑
     *
     * @param modelProduceLine
     * @return
     */
    @ApiOperation(value = "产品线-编辑", notes = "产品线-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody ModelProduceLine modelProduceLine) {
        boolean result = false;

        if (modelProduceLine.getId() == 0L) {
            QueryWrapper<ModelProduceLine> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("Produce_Line_No", modelProduceLine.getProduceLineNo());
            queryWrapper.or().eq("Produce_Line_Name", modelProduceLine.getProduceLineName());
            ModelProduceLine mmMaterialdtl1 = modelProduceLineService.getOne(queryWrapper);
            if (mmMaterialdtl1 != null) {
                String msg = StrUtil.format("编号为{}或者名稱为{}的记录已经存在！", mmMaterialdtl1.getProduceLineNo(),mmMaterialdtl1.getProduceLineName());
                throw new ApiException(msg);
            }
            result = modelProduceLineService.save(modelProduceLine);
        } else {
            result = modelProduceLineService.updateById(modelProduceLine);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "产品线-通过id删除", notes = "产品线-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name = "id", required = true) String id) {
        modelProduceLineService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "产品线-批量删除", notes = "产品线-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("ModelProduceLineIDs") @RequestBody List<Long> ids) {
        this.modelProduceLineService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "产品线-通过id查询", notes = "产品线-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ModelProduceLine modelProduceLine = modelProduceLineService.getById(id);
        return CommonResult.success(modelProduceLine);
    }

    /**
     * 编辑包括关系表
     *
     * @param modelProduceLineVo
     * @return
     */
    @ApiOperation(value = "产线-编辑", notes = "产线-编辑")
    @PostMapping(value = "/saveRelation")
    public CommonResult<?> saveRelation(@RequestBody ProduceLineDto modelProduceLineVo) {
        boolean result = false;
        if (modelProduceLineVo != null) {
            ModelProduceLine modelProduceLine = new ModelProduceLine();
            BeanUtils.copyProperties(modelProduceLineVo, modelProduceLine);
            if (modelProduceLine.getId() == 0L) {
                result = modelProduceLineService.save(modelProduceLine);
            } else {
                result = modelProduceLineService.updateById(modelProduceLine);
            }
            ModelProduceLineBranchLine modelProduceLineBranchLine = new ModelProduceLineBranchLine();
            modelProduceLineBranchLine.setProduceLineId(modelProduceLine.getId());
            modelProduceLineBranchLine.setSourceLineId(modelProduceLineVo.getSourceLineId());
            modelProduceLineBranchLine.setSourceLineType(modelProduceLineVo.getSourceLineType()); //表示是来源工序线类型1:产品;2:生产

            if( modelProduceLineVo.getRelationId() != null  && modelProduceLineVo.getRelationId()> 0) {
                modelProduceLineBranchLine.setId(Convert.toLong(modelProduceLineVo.getRelationId()));
                result = modelProduceLineBranchLineService.updateById(modelProduceLineBranchLine);
            }else{
                result = modelProduceLineBranchLineService.save(modelProduceLineBranchLine);
            }
        }
        return CommonResult.success(result);
    }
}
